#include "../include/EventLocker.h"
#include <stddef.h>

using namespace boost::interprocess;
using namespace boost::posix_time;

namespace Polaris_CommonLib
{

CEventLocker::CEventLocker(void)
{
	//m_hEvent = CreateEvent( NULL, TRUE, FALSE, TEXT(""));
}


CEventLocker::~CEventLocker(void)
{
	//CloseHandle( m_hEvent );
}

// Wait
// Params:
// a_TimeMS:等待時間(以ms計)
// Retures:
// true : 事件已觸發
// false : 等待時間已過，事件仍未觸發
bool CEventLocker::Wait( unsigned long a_TimeMS )
{
	//DWORD result = ::WaitForSingleObject( m_hEvent, a_TimeMS );
	boost::interprocess::scoped_lock<interprocess_mutex> lock(this->m_Mutex);
	boost::posix_time::ptime wt( second_clock::universal_time());
	wt += milliseconds( a_TimeMS );
	this->m_Cond.timed_wait( lock, wt );
	//return result == WAIT_OBJECT_0;
	return true;
}

void CEventLocker::Set()
{
	//::SetEvent( m_hEvent );
	this->m_Cond.notify_one();
}

void CEventLocker::Reset()
{
	//::ResetEvent( m_hEvent );
	//this->m_Mutex.lock();
}

}
